package com.tj.shopping.web.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/queryTrade")
public class TradeQueryController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCv6X8VEcVZOWkVPfQ9CYtBNacLPyRDculbYrADG7JS7heMhqaMhrFL/feya0kkZUPWlFyjFpQEJX6RpaCTAWLWDgFfGidJ+bahiLZnOfZevFXM2Z92vNN2zkgs6Y8uAbEcdehUDCTrBCOGkQWRT5Q1Q5PitxPda9PGP6gVQPwcmk+vtsFeTAUo+Sv9l2KN5+B9tSXw2XyQQA2j8M5ahcSNMvJ61HlwyBYdadhU8gpi7ZCHTj5UThYyeas/YL6quys/Fse1CabrXlEpSzeVzCe6eWgdnsNuTCc5Z/XKwAFFePCFlTqOcA9cLN/KYZFp7qgfHfmvJjGh4qpNIou0qzmjAgMBAAECggEBAIlctB5YtRi6CsNKkHhFBpdR9PGESaXdijkQ885Jh1LYlkzmLBFZcAKd+xg4THH+HR3m1fuHuucN/CpxAdUo5OuvHgUMIBfUxXaXkY9DpI1PVVRTrwCB7EI+S/++FU7ghVNrApL2tLFLlw4SGh3g3SNGdKk5OmrRMxoARRjrmLIfxZ8Zre9jpjL14Ka+Pcs5iGr7vFPsviOZ8ZNsa9LO4so407bUL66ctHCmTEmXHcJ+X4sYkKuCHCmzEtCckyJ9hdaISd/WB4RaZ0sumHKGCA06zIGs0pLyUG73MYRggVzf2hZs4h3LW2Mqov2qxaYSxwutL3/VKiONM3ZECQK5H/ECgYEA1q5YMsynZ9eEeLYmlxFxvcR/gem/bpXi4Y4TQHq6vTsbr60svlDyJJ+mHeoP7a6dJllBJLYo3OttbKqOH8OWvsrM0/dsFzWvA66Kufz0ZbKKJXWRzgT5NwxChT55y9PF6y1cogHo6GPvPFEONyuIJ5bsASy0ZvLEpowoiUEvb+sCgYEA0cTyx3OLn9dkEGqbumfPS8QUPiy5jBKlOLRpskacF7pnWbI8wExCrmBxCcMGNodmyl6YJi2znCMCUSl35xZ7a2Nx7LJV3+WeWyxarDlmGDTurSk8r4nYbS0NleiIwKe8D3uxjBgvCxVdaIYMSUa3otdsN+KpxOrrdHTd8IIwpykCgYAyWePPzlY5McAdO8OjTPEbbBnn03YVrsvu0Q7M4bwoo9Njib9jx8JzOoqQm5Quz/SFyQtRW/QLlKAui3YYFxylFRWjZXWRoAKzX12kL6L2s0BZdtDCvEHjWlnDvJp+ZuYVuabvTHlfAAldYTlvUSN2/ZvOzrhlKO1cjqT3RDWnDwKBgHnKITLP0QbypTQq0VN+pgpVQ3N9wBSlVqv/g0ZwXHwiS5L2OvNi02wurjJ+uoK6RmHEXg38hsVmRhgdPXI7yVhmGmTUfatKw5AnxlUr1izDvrdms8cu/ysT90lMAunn+PX8WO7/s4B53QusmEGK/19aN4iS3wJnamDo4VqcG+dRAoGBAKZaHYA0WIQqjcqgmm00SLyiogs8MzONAAgkvYHPqvR11Qp3xh2PyRBufY8L4rxqq9rNMMTEzbZPjRcyvdLkXveVBdDDiVy6KIF+MpWC4rYKM8mOyat4pp9PxhaKbcVrW/xxz0EaTDjZfsl0kAs+DWVz6CrggaKlayDZYwkk+Iwa";//这几部分都是自己的信息 私钥
            String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmy6+mv9P8caLnnTJZXHMelhLc+71aOjTxS6l3CtBPCHW12YzKfRkcXF+L6xM8zLWp1o5znzJT50TvWVUaZ1iCWN7M+HiewSKOhAxBR3x8PFRDwlS0xgXkKWX5Aq0419CJJgK9nfSP3q0dK3iVjk8KcoJKDuPjP0lljgOD5Py/bheS8DIYhP4ChSIBDxFrhS/NCxENGCfhCVFmMVAJ+Y04SSrdtLwNNZpqIDswdYXDGVYVlRNvHyA9xiuhA/jWmR4NfIX0jW8y8kPv/izOV9i9mJ4xXMNUceqKkikDjCp6bLge/qTMZ9W7EaxQsqnaTGOlT9VXyhWPsLPKcl+TZNYLwIDAQAB";   //    公钥
			AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
            alipayConfig.setAppId("9021000133640018");//
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setFormat("json");
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");

            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();

            // 从请求中获取参数tradeNo
            String tradeNo = req.getParameter("trade_no");
            model.setOutTradeNo(tradeNo);
            request.setBizModel(model);
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            System.out.println(response);
            System.out.println("response.getBody() ==== " +response.getBody());
            System.out.println(response.isSuccess());
            if (response.isSuccess()){
                System.out.println("支付成功");
                resp.setStatus(200);
                PrintWriter writer = resp.getWriter();
                writer.write(response.getBody());
            }else{
                resp.setStatus(255);
                System.out.println("支付失败");
            }
//            PrintWriter writer = resp.getWriter();
//            writer.write(response.getBody());
//            resp.getWriter();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

    }

}
